#include "Base/CommonFunction.h"

int getThreadID()
{
#ifdef __WIN_PLATFORM__
	return (int)GetCurrentThreadId();
#elif __LINUX_PLATFORM__ 
	return (int)pthread_self();
#endif
}

time_t getTimestamp()
{
	time_t t;
#ifdef __WIN_PLATFORM__
	time(&t);
	return t;
#elif __LINUX_PLATFORM__ 
	struct timeval tm;
	gettimeofday(&tm, NULL);
	t = tm.tv_sec;
	return t;
#endif
}

unsigned long long getMTimestamp()
{
	// windows
#ifdef WIN32
	return GetTickCount64();

	// linux
#else
	struct timeval tm;
	gettimeofday(&tm, NULL);

	unsigned long t = tm.tv_sec * 1000 + tm.tv_usec / 1000;

	return t;

#endif

}

int fls(int x)
{
	int position;
	int i;
	if (0 != x)
	{
		for (i = (x >> 1), position = 0; i != 0; ++position)
		{
			i >>= 1;
		}
	}
	else
	{
		position = -1;
	}
	return position + 1;
}

int fls64(unsigned long long x)
{
	unsigned int h = x >> 32;
	if (h)
	{
		return static_cast<int>(fls(h) + 32);
	}
	return fls(static_cast<int>(x));
}

unsigned int flsLong(unsigned long long l)
{
	if (sizeof(l) == 4)
	{
		return fls(static_cast<int>(l));
	}
	return fls64(l);
}

unsigned int roundupPowOf2(unsigned long long x)
{
	return 1UL << flsLong(x - 1);
}
